﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using xmlTools;
using ClassLibraryMessageDataType;
namespace GUI
{
    public partial class FormWorkStepProjectsHairDown_demo : Form
    {
        public FormWorkStepProjectsHairDown_demo()
        {

            InitializeComponent();
        }
        public string [] filesG = null;
        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog openfile = new System.Windows.Forms.FolderBrowserDialog();
#if false
            if (openfile.ShowDialog() == DialogResult.OK)
            {
                string filesnamepath = openfile.SelectedPath;
                // dic1.Add(n, filename);
                // fileArr[n].Text = filename.Substring(filename.LastIndexOf("\") + 1, filename.LastIndexOf(".") - (filename.LastIndexOf("\") + 1)); //filesnamepath = "F:\\workProjects"
                string[]files= Directory.GetFiles(filesnamepath);
                filesG = files;
                treeView1.Nodes.Clear();
                foreach(var asr in files)
                {
                    treeView1.Nodes.Add(Path.GetFileName( asr));                     
                }
            
            }else
            {

            }
#else
            //string[] files = Directory.GetFiles("F:\\workProjects");
            string[] files = Directory.GetFiles("C:\\Users\\llskj\\Desktop\\上位机开发准备\\充放电测试");
            filesG = files;
            treeView1.Nodes.Clear();
            foreach (var asr in files)
            {
                treeView1.Nodes.Add(Path.GetFileName(asr));
            }

#endif
        }
        string slecpath = string.Empty;
        string slcname = string.Empty;

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode secNode = e.Node;
            string getss = secNode.Text;
            foreach( var  PAS in filesG)
            {
               if( Path.GetFileName(PAS) == getss)
                {
                    textBox1.Clear();
                    textBox1.Text = File.ReadAllText(PAS);
                    label2.Text = getss;
                    slecpath = PAS;
                    slcname = getss;
                    break;
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
          
        }
    
        public ClassMiddleClientBusiness[] allMiddleClientBussiness = null;

        public  void showClient()
        {
  
            if (allMiddleClientBussiness!=null)
            {
                allMiddleClientBussiness[0].outMiddleclientBusinessStatus(out MiddleclientBusinessStatus outs1);
                 if(outs1  == MiddleclientBusinessStatus.free)
                {
                    checkBoxcb1.Enabled = true;
                }
                else
                {
                    checkBoxcb1.Enabled = false; checkBoxcb1.ForeColor = Color.DarkRed;
                }

                allMiddleClientBussiness[1].outMiddleclientBusinessStatus(out MiddleclientBusinessStatus outs2);
                if (outs2 == MiddleclientBusinessStatus.free)
                {
                    checkBoxcb2.Enabled = true;
                }
                else
                {
                    checkBoxcb2.Enabled = false; 
                }


                allMiddleClientBussiness[2].outMiddleclientBusinessStatus(out MiddleclientBusinessStatus outs3);
                if (outs3 == MiddleclientBusinessStatus.free)
                {
                    checkBoxcb3.Enabled = true;
                }
                else
                {
                    checkBoxcb3.Enabled = false; 
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("先选择方案");
                return;
            }
            else if (textBox2.Text == "")
            {
                MessageBox.Show("先选择机柜");
                return;
            }else
            {
                ClassWorkStepsProjects workpros = XMLClass.XmlDeserialize <ClassWorkStepsProjects>(slecpath, slcname);
                ClassWorkStepsCollections[] worksteps     = new ClassWorkStepsCollections[workpros.workstepsProjets.Count];
                for(int a =0;a<workpros.workstepsProjets.Count;a++)
                {
                    worksteps[a] = new ClassWorkStepsCollections();
                    worksteps[a].noUsed1 = 0x03;
                    worksteps[a].workStepsType =  workpros.workstepsProjets[a].工步类型 ;
                    worksteps[a].workStepsCode = workpros.workstepsProjets[a].工步号;
                    worksteps[a].constantVoltage = ClassBigLittleConver.The16bitFrontnextChange( workpros.workstepsProjets[a].恒压);
                    worksteps[a].constantCurrent = ClassBigLittleConver.The16bitFrontnextChange(workpros.workstepsProjets[a].恒流);
                    worksteps[a].minimumVoltage = ClassBigLittleConver.The16bitFrontnextChange(workpros.workstepsProjets[a].下限电压);
                    worksteps[a].maximumVoltage = ClassBigLittleConver.The16bitFrontnextChange(workpros.workstepsProjets[a].上限电压);
                    worksteps[a].endCurrent = ClassBigLittleConver.The16bitFrontnextChange(workpros.workstepsProjets[a].终止电流);
                    worksteps[a].maximumCurrent = ClassBigLittleConver.The16bitFrontnextChange(workpros.workstepsProjets[a].上限电流);
                    worksteps[a].endCapacity = ClassBigLittleConver.The16bitFrontnextChange(workpros.workstepsProjets[a].终止容量);
                    worksteps[a].limitTime = ClassBigLittleConver.The16bitFrontnextChange(workpros.workstepsProjets[a].限定时间);
                }

                if(allMiddleClientBussiness!=null)
                {
                    string allcstr = textBox2.Text;
                    string[] allstrarry = allcstr.Split(';');
                    foreach (string clinet in allstrarry)
                    {
                        if (clinet != "")
                        {
                            string ipreal = "192.168.2." + clinet;
                            for (int a = 0; a < allMiddleClientBussiness.Length; a++)
                            {
                                allMiddleClientBussiness[a].outMiddleclientBusinessStatus(out MiddleclientBusinessStatus outs);
                                if (ipreal == allMiddleClientBussiness[a].middleClientip && outs == MiddleclientBusinessStatus.free)
                                {
                                    ClassWorkStepsCollections lastWorkStepStart = new ClassWorkStepsCollections();
                                    lastWorkStepStart.workStepsType = 0x10;
                                    allMiddleClientBussiness[a].PushWsIntoDquenue(ref lastWorkStepStart);

                                    allMiddleClientBussiness[a].PushWsIntoDquenueArry(ref worksteps);

                                    ClassWorkStepsCollections lastWorkStepwaitforclasifeild = new ClassWorkStepsCollections();
                                    lastWorkStepwaitforclasifeild.workStepsType = 0x0a;
                                    allMiddleClientBussiness[a].PushWsIntoDquenue(ref lastWorkStepwaitforclasifeild);
                                    break;

                                }else
                                {
                                    
                                }
                            }
                        }else
                        {

                        }
                    }
                }

            }

            this.Close();
            
        }

        private void checkBoxcb1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBoxcb1.CheckState == CheckState.Checked)
            {
                textBox2.AppendText("1;");
            }else
            {
                textBox2.Text= textBox2.Text.Replace("1;", "");
            }
        }

        private void FormWorkStepProjectsHairDown_demo_FormClosed(object sender, FormClosedEventArgs e)
        {
           
        }

        private void FormWorkStepProjectsHairDown_demo_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void checkBoxcb2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxcb2.CheckState == CheckState.Checked)
            {
                textBox2.AppendText("2;");
            }
            else
            {
                textBox2.Text = textBox2.Text.Replace("2;", "");
            }
        }

        private void checkBoxcb3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxcb3.CheckState == CheckState.Checked)
            {
                textBox2.AppendText("3;");
            }
            else
            {
                textBox2.Text = textBox2.Text.Replace("3;", "");
            }
        }
    }
}
